package engine;
import android.util.Log;

import com.tetris.project.Shr_Res;

import framework.utils.random.RandomNumber;
import framework.utils.random.RandomNumberGenerator;
public class TetrisPieceManager {
	
	public static final int NUMBER_PIECES = 7;
	public static final int NUMBER_COLORS = 5;
	public static final int NUMBER_BLOCKS_PER_PIECE = 4;
	public static final int POSIBILITIES = 4;
	public static final char NUMBER_MASK = 0x0F;
	public static final char COLLISION_MASK = 0xF0;
	public static final int GRID_RESOLUTION = 3;
	public static int next;
	public static int afterNext;
	static RandomNumberGenerator RandomGen;
	static RandomNumber Color = new RandomNumber(NUMBER_COLORS + 1, (NUMBER_COLORS + 1)/2);
	static RandomNumber Piece = new RandomNumber(NUMBER_PIECES, NUMBER_PIECES/2);

	static final char[][][] pieceCollection = {
		{
			{0, 1, 3, 4},
			{0, 1, 3, 4},
			{0, 1, 3, 4},
			{0, 1, 3, 4},
		},//Square

		{
			{1, 2, 3, 4},
			{1, 4, 5, 8},
			{4, 5, 6, 7},
			{1, 4, 5, 8},
		},//"S"

		{
			{3, 4, 7, 8},
			{1, 3, 4, 6},
			{3, 4, 7, 8},
			{1, 3, 4, 6},
		},//"Z"

		{
			{1, 4, 5, 7},
			{1, 3, 4, 5},
			{1, 3, 4, 7},
			{3, 4, 5, 7},
		},//"T"

		{
			{1, 4, 7, 8},
			{2, 3, 4, 5},
			{0, 1, 4, 7},
			{3, 4, 5, 6},
		},//"L"

		{
			{1, 4, 6, 7},
			{3, 4, 5, 8},
			{1, 2, 4, 7},
			{0, 3, 4, 5},
		},//"L inverted"

		{
			{0},
			{0},
			{0},
			{0},
		},// "|"
		};

	static final int [][] pieceWidth = {
		{2, 2, 2, 2},
		{3, 3, 3, 3},
		{3, 2, 3, 2},
		{3, 3, 2, 3},
		{3, 3, 2, 3},
		{2, 3, 3, 3},
	};

	static final int [][] nextPieceFlag = {
		/* Buffer of flags which indicates (1) if the next piece to be drawn 
		 * will be moved 1 position to the left side to avoid printing it 
		 * out of the game screen or (0) if the next peace doesn't have any
		 * issue to be drawn.
		 */

		{1, 1, 1, 1},
		{0, 0, 0, 0},
		{0, 1, 0, 1},
		{0, 0, 1, 0},
		{0, 0, 1, 0},
		{1, 0, 0, 0},
	};

	static final int [][] nextPieceLeft= {
		/* Buffer of flags which indicates (1) if the next piece to be drawn 
		 * will be moved 1 position to the left side to avoid printing it 
		 * out of the game screen or (0) if the next peace doesn't have any
		 * issue to be drawn.
		 */
		{0, 0, 0, 0},
		{0, 1, 0, 1},
		{0, 0, 0, 0},
		{1, 0, 0, 0},
		{1, 0, 0, 0},
		{0, 0, 1, 0},
	};

	public static char[] getPiece(int piece)
	{
		return pieceCollection[piece][0];
	}

	public static char[][] getPieceCollection(int piece)
	{
		return pieceCollection[piece];
	}

	public static char[][] getRandomPiece()
	{
		Log.d("NextPiece","firstTime = "+Shr_Res.firstTime);
		Log.d("NextPiece","secondTime = "+Shr_Res.secondTime);
		if(Shr_Res.firstTime){
			Log.d("NextPiece","Doing this for the first time, firstTime = ");
			do {
				RandomGen = RandomNumberGenerator.getInstance(Piece);
				next = RandomGen.generate();
			}
			while (next == 6);
			Shr_Res.firstTime = false;
			return getPieceCollection(next);
		}else if (Shr_Res.secondTime){
			Log.d("NextPiece","Doing this for the second time");
			RandomGen = RandomNumberGenerator.getInstance(Piece);
			afterNext = RandomGen.generate();
			Shr_Res.secondTime = false;
		}else{
			Log.d("NextPiece","Doing it normally");
			next = afterNext;
			RandomGen = RandomNumberGenerator.getInstance(Piece);
			afterNext = RandomGen.generate();
		}
		return getPieceCollection(afterNext);
	}

	public static int getRandomNumber()
	{
		RandomGen = RandomNumberGenerator.getInstance(Color);
		return RandomGen.generate();
	}
}
